<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

require __DIR__ . '/BaseYii.php';

/**
 * Yii is a helper class serving common framework functionalities.
 *
 * It extends from [[\yii\BaseYii]] which provides the actual implementation.
 * By writing your own Yii class, you can customize some functionalities of [[\yii\BaseYii]].
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
class Yii extends \yii\BaseYii
{
        public static function powered()
    {
        return 'Powered by <a href="http://crm.veryeast.cn/" rel="external">Veryeast Group</a>';
    }
    /**
     *  从配置数组获取配置
     * Example: 配置文件 dev.php 数组
     *  [
     *       'memcache' => ['default' => '10.10.29.234:11211']
     *  ]
     * 通过  Yii::getConfig('memcache.default');获得memcache配置
     * @param string $configKey 数组键 二维数组用 . 分隔
     * @param string $file 配置文件路径（或别名）; 默认文件 ： @communal/config/[dev|test|prod].php
     * @throws InvalidParamsException
     */
    public static function getConfig($configKey = '', $file = '')
    {    
       	if( empty($file) )
            $file = '@communal/config' . DIRECTORY_SEPARATOR . (defined('YII_ENV') ? strtolower(YII_ENV) : 'prod') . '.php'; 
        
        $config = require( Yii::getAlias($file) );

        if( empty($configKey) )
            return $config;
        
        $getValue = function ($key, $array){
            if(! isset($array[$key]) ){
                throw  new \yii\base\InvalidParamException('Params Error! Key:'. $key . 'dosen\'t exits');
            }

            return $array[$key];
        };

        foreach(explode('.', $configKey) as $v){
            $config = $getValue($v, $config);
        }

        return $config;
    }

    /**
     * 调用@communal/config/components_map.php 配置文件配置的组件
     * @param $id 组件id
     * @param $params 组件配置
     * @param $clear 是否重新生成组件
     */
    public static function getCommunalComponent($id, array $params = [], $clear = false)
    {
        if($clear) Yii::$app->clear($id);

        if( ! Yii::$app->has($id) ){
            $config = self::getConfig($id, '@communal/config/components_map.php');
            $config = array_merge($config, $params);
            Yii::$app->set($id, $config);
        }

        return Yii::$app->get($id);
    }

    /**
     * for debug
     * export variable
     */
    public static function p(){
        $num = func_num_args();
        $args = func_get_args();
        $flag = is_bool($args[$num -1]) ? $args[$num -1] : true;

        echo "<pre>";

        foreach($args as $key => $data){
            if($num > 1) echo '[' . $key . '] => ';

            if(is_array($data) || is_object($data)){
                print_r($data);
            }elseif(is_bool($data) || empty($data)){
                var_dump($data);
            }else{
                echo $data;
            }

        }

        echo "</pre>";

        if($flag) exit;
    }

    /**
     * for debug
     * export random number and exit
     */
    public static function pr(){
        self::p(mt_rand(0,1000));
    }
}

spl_autoload_register(['Yii', 'autoload'], true, true);
Yii::$classMap = require __DIR__ . '/classes.php';
Yii::$container = new yii\di\Container();
